<!--
  #%L
  thinkbig-ui-feed-manager
  %%
  Copyright (C) 2017 ThinkBig Analytics
  %%
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  
      http://www.apache.org/licenses/LICENSE-2.0
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  #L%
  -->
<card-layout body-css="md-padding">
  <header-section>
    <div layout="row" layout-align="space-between-center" flex>
      <div class="card-title" flex> {{"views.define-feed-schedule.Schedule" | translate}} </div>

      <div class="card-sub-header">{{vm.stepNumber}} {{"views.define-feed-general-info.of" | translate}} {{vm.totalSteps}}</div>
    </div>
    <div layout="row" layout-align="space-between-center" flex class="layout-padding-top">
      <div class="card-sub-header" flex> {{"views.define-feed-schedule.SAETF" | translate}} </div>
      <thinkbig-feed-errors-card-header></thinkbig-feed-errors-card-header>
    </div>
  </header-section>
  <body-section md-content>
    <ng-form name="vm.defineFeedScheduleForm">
      <div layout-gt-sm="row" style="margin-bottom: 18px">

        <md-input-container class="md-block" flex>
          <label>{{"views.define-feed-schedule.Strategy" | translate}}</label>
          <md-select ng-model="vm.model.schedule.schedulingStrategy" ng-change="vm.onScheduleStrategyChange()" ng-disabled="vm.model.view.schedule.disabled || vm.model.view.schedule.schedulingStrategy.disabled">
            <md-option ng-value="opt.value" ng-repeat="opt in vm.scheduleStrategies">{{ opt.label }}</md-option>
          </md-select>
        </md-input-container>

        <md-input-container class="md-block" flex ng-if="vm.model.schedule.schedulingStrategy == 'CRON_DRIVEN'">
          <label>{{"views.define-feed-schedule.Expression" | translate}}</label>
          <input required ng-model="vm.model.schedule.schedulingPeriod" name="cronExpression" ng-disabled="vm.model.view.schedule.disabled || vm.model.view.schedule.schedulingPeriod.disabled"
                 cron-expression-validator
                 ng-model-options="{ updateOn: 'default blur', debounce: {'default': 300, 'blur': 0} }"/>

          <div ng-show="vm.defineFeedScheduleForm.cronExpression.$touched && vm.defineFeedScheduleForm.cronExpression.$invalid"
               ng-messages="vm.defineFeedScheduleForm.cronExpression.$error">
            <div ng-show="vm.defineFeedScheduleForm.cronExpression.$error.cronExpression"
                 ng-message="cronExpression"> {{"views.define-feed-schedule.ICE" | translate}}
            </div>
            <div ng-show="vm.defineFeedScheduleForm.cronExpression.$error.required" ng-message="required">
              {{"views.define-feed-general-info.Req" | translate}}
            </div>
          </div>
          <cron-expression-preview cron-expression="vm.model.schedule.schedulingPeriod"
                                   ng-if="vm.model.schedule.schedulingStrategy == 'CRON_DRIVEN' && vm.defineFeedScheduleForm.cronExpression.$valid"></cron-expression-preview>
        </md-input-container>
        <div flex ng-if="vm.model.schedule.schedulingStrategy == 'TIMER_DRIVEN' || vm.model.schedule.schedulingStrategy == 'PRIMARY_NODE_ONLY'" layout="row" style="margin-bottom:-18px">
          <md-input-container class="md-block" flex>
            <label>{{"views.define-feed-schedule.RS" | translate}}</label>
            <input ng-model="vm.timerAmount" ng-disabled="vm.model.view.schedule.disabled || vm.model.view.schedule.schedulingStrategy.disabled" required string-to-number type="number" ng-change="vm.timerChanged()"  ng-model-options="{ updateOn: 'default blur', debounce: {'default': 500, 'blur': 0} }"/>
          </md-input-container>

          <md-input-container class="md-block" flex >
            <label>{{"views.define-feed-schedule.Units" | translate}}</label>
            <md-select ng-model="vm.timerUnits" ng-change="vm.timerChanged()" ng-disabled="vm.model.view.schedule.disabled || vm.model.view.schedule.schedulingStrategy.disabled">
              <md-option value="days"> {{"views.define-feed-schedule.Days" | translate}} </md-option>
              <md-option value="hrs"> {{"views.define-feed-schedule.Hours" | translate}} </md-option>
              <md-option value="min"> {{"views.define-feed-schedule.Minutes" | translate}} </md-option>
              <md-option value="sec"> {{"views.define-feed-schedule.Seconds" | translate}} </md-option>
            </md-select>
          </md-input-container>
        </div>

      </div>
      <!--
          <md-input-container class="md-block" >
              <label> {{"views.define-feed-schedule.CT" | translate}} </label>
              <input type="number" ng-model="vm.model.schedule.concurrentTasks">
              </input>
          </md-input-container>
          -->
      <div ng-if="vm.isClustered && vm.supportsExecutionNode" style="margin:18px 0">
        <md-input-container class="md-block">
          <label> {{"views.define-feed-schedule.EN" | translate}} </label>
          <md-select ng-model="vm.model.schedule.executionNode" required ng-disabled="vm.model.view.schedule.disabled || vm.model.view.schedule.executionNode.disabled">
            <md-option value="ALL"> {{"views.define-feed-schedule.AN" | translate}} </md-option>
            <md-option value="PRIMARY"> {{"views.define-feed-schedule.PN" | translate}} </md-option>
          </md-select>
        </md-input-container>
      </div>

      <div ng-if="vm.model.schedule.schedulingStrategy == 'TRIGGER_DRIVEN'">
        <div layout="column">
          <div class="md-subhead"> {{"views.define-feed-schedule.RP" | translate}} </div>
          <div class="hint"> {{"views.define-feed-schedule.SED" | translate}} </div>
        </div>
        <div layout="column" layout-align="start start" class="layout-padding-top-bottom">

          <div ng-if="vm.model.schedule.preconditions != null && vm.model.schedule.preconditions.length >0 " layout-fill>
            <md-list>
              <md-list-item ng-repeat="rule in vm.model.schedule.preconditions track by $index" layout-fill class="layout-padding-top-bottom">
                <div layout="column" flex class="md-list-item-text">
                  <span>{{rule.propertyValuesDisplayString}}</span>
                  <span class="hint">{{rule.name}}</span>
                </div>
                            <span class="md-secondary">
                                  <md-button class="md-icon-button" ng-click="vm.showPreconditionDialog($index)">
                                    <ng-md-icon icon="edit"></ng-md-icon>
                                  </md-button>
                            </span>
                <md-divider ng-if="!$last"></md-divider>
              </md-list-item>
            </md-list>
          </div>
          <md-button class="md-primary" ng-click="vm.showPreconditionDialog()" ng-disabled="vm.model.view.schedule.disabled || vm.model.view.schedule.preconditions.disabled">
            {{"views.define-feed-schedule.AP" | translate}}
          </md-button>
        </div>

      </div>

      <div layout="column">
        <md-checkbox ng-model="vm.model.active" style="margin-top:18px" ng-disabled="vm.model.view.schedule.disabled || vm.model.view.schedule.active.disabled"> {{"views.define-feed-schedule.EFI" | translate}}</md-checkbox>
        <span class="hint fade" ng-show="vm.model.active" ng-hide="!vm.model.active">{{"views.define-feed-schedule.TFW" | translate}}</span>
        <span class="hint fade" ng-show="!vm.model.active" ng-hide="vm.model.active">{{"views.define-feed-schedule.TFW2" | translate}}</span>
      </div>


      <thinkbig-step-buttons can-continue="vm.isValid && vm.defineFeedScheduleForm.$valid &&  !vm.savingFeed" step-index="{{::vm.stepIndex}}" final-step='{{"views.define-feed-schedule.Create" | translate}}' on-click-final-button="vm.createFeed()"></thinkbig-step-buttons>

    </ng-form>
  </body-section>
</card-layout>

